package com.peakmain.homemodule;

import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.peakmain.baselibrary.recyclerview.view.WrapRecyclerView;
import com.peakmain.homemodule.activity.SearchActivity;
import com.peakmain.homemodule.activity.SearchCityActivity;
import com.peakmain.homemodule.adapter.HomeArticleAdapter;
import com.peakmain.homemodule.banner.GlideImageLoader;
import com.peakmain.homemodule.base.BaseHomeFragment;
import com.peakmain.homemodule.contarct.HomeArticleContract;
import com.peakmain.homemodule.presenter.HomeArticlePresenter;
import com.peakmain.homemodule.view.SlidingMenu;
import com.peakmain.wanandroid.netmodule.bean.home.HomeArticleBean;
import com.peakmain.wanandroid.netmodule.bean.home.HomeBannerBean;
import com.peakmain.wanandroid.netmodule.bean.home.HomeFriendBean;
import com.peakmain.wanandroid.netmodule.bean.home.HomeTopArticleBean;
import com.peakmain.wanandroid.netmodule.constant.Constant;
import com.peakmain.wanandroid.netmodule.utils.XRefreshUtils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: peakmain
 * createdata：2019/7/11
 * mail: 2726449200@qq.com
 * desiption:
 */
public class HomeFragment extends BaseHomeFragment<HomeArticlePresenter> implements HomeArticleContract.View, SlidingMenu.OpenOrCloseMenu, View.OnClickListener {

    @BindView(R2.id.recycler_view)
    protected WrapRecyclerView mRecyclerView;
    @BindView(R2.id.xrefreshview)
    protected XRefreshView mXrefreshView;
    private HomeArticleAdapter mAdapter;
    private int page = 0;
    private int totalPage = 1;
    private List<String> images;
    private Banner mBanner;
    private View mBannerView;
    List<HomeArticleBean.DatasBean> mDatas;
    private TextView mTvLocation;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected void initView(View view) {
        mTvLocation = view.findViewById(R.id.tv_location);
        mTvLocation.setOnClickListener(this);
        initRecyclerView();
    }

    private void initRecyclerView() {
        XRefreshUtils.getInstance().initXRefresh(mXrefreshView, getContext());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        mXrefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                getList(true, false);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                getList(false, true);
            }
        });
    }

    private void getList(boolean isPullDown, boolean isSilence) {
        if (isPullDown) {
            page = 0;
        }
        if (isSilence) {
            if (page > totalPage) {
                return;
            }
        }
        mPresenter.getHomeArticle(page, isPullDown, isSilence);
    }

    @Override
    public void onResume() {
        super.onResume();

        if (mXrefreshView != null) {
            mXrefreshView.startRefresh();
        }
        mPresenter.getHomeBanner();
        mPresenter.getHomeTopArticle();
        getHomeFriend();
    }

    public void getHomeFriend() {
    }

    @Override
    public void showHomeArticleSuccess(final HomeArticleBean datasBean, boolean isPullDown, boolean isSilence) {
        mXrefreshView.stopLoadMore();
        mXrefreshView.stopRefresh();
        setData(datasBean);
    }

    @Override
    public void showHomeBannerSuccess(final List<HomeBannerBean> data) {
        if (data != null && data.size() > 0) {
            mBannerView = LayoutInflater.from(getContext()).inflate(R.layout.banner_home, mRecyclerView, false);
            mBanner = mBannerView.findViewById(R.id.banner);
            images = new ArrayList<>();
            List<String> titles = new ArrayList<>();
            for (int i = 0; i < data.size(); i++) {
                HomeBannerBean dataBean = data.get(i);
                images.add(dataBean.getImagePath());
                titles.add(dataBean.getDesc());
            }
            //设置banner样式
            mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
            //设置图片加载器
            mBanner.setImageLoader(new GlideImageLoader());
            //设置图片集合
            mBanner.setImages(images);
            //设置banner动画效果
            mBanner.setBannerAnimation(Transformer.DepthPage);
            //设置标题集合（当banner样式有显示title时）
            mBanner.setBannerTitles(titles);
            //设置自动轮播，默认为true
            mBanner.isAutoPlay(true);
            //设置轮播时间
            mBanner.setDelayTime(3000);
            //设置指示器位置（当banner模式中有指示器时）
            mBanner.setIndicatorGravity(BannerConfig.CENTER);
            //banner设置方法全部调用完毕时最后调用
            mBanner.start();
            //设置点击事件
            mBanner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int position) {
                    String url = data.get(position).getUrl();
                    startBrowserActivity(getContext(), Constant.MODE_DEFAULT, url);
                }
            });

        }
    }

    @Override
    public void showHomeTopArticle(List<HomeTopArticleBean> datas) {
        if (datas != null && datas.size() > 0) {
            mDatas = new ArrayList<>();
            HomeArticleBean.DatasBean datasBean;
            for (HomeTopArticleBean data : datas) {
                datasBean = new HomeArticleBean.DatasBean();
                datasBean.setAuthor(data.getAuthor());
                datasBean.setLink(data.getLink());
                datasBean.setTitle(data.getTitle());
                datasBean.setType(data.getType());
                datasBean.setNiceDate(data.getNiceDate());
                datasBean.setSuperChapterName(data.getSuperChapterName());
                datasBean.setChapterName(data.getChapterName());
                mDatas.add(datasBean);
            }

        }

    }


    private void setData(final HomeArticleBean datasBean) {
        page = datasBean.getCurPage();
        totalPage = datasBean.getPageCount();
        if (mDatas == null) {
            mDatas = new ArrayList<>();
        }
        mDatas.addAll(datasBean.getDatas());
        mAdapter = new HomeArticleAdapter(getContext(), mDatas);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addHeaderView(mBannerView);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mBanner != null) {
            mBanner.stopAutoPlay();
        }
    }

    @OnClick({R2.id.ll_search})
    public void click(View view) {
        if (view.getId() == R.id.ll_search) {
            startActivity(new Intent(getContext(), SearchActivity.class));
        }
    }

    @Override
    public void showFriendDataSuccess(final List<HomeFriendBean> dataBeans) {

    }

    @Override
    public void openMenu() {
        mPresenter.getFriendData();
        if (mBanner != null) {
            mBanner.stopAutoPlay();
        }
    }

    @Override
    public void closeMenu() {
        if (mBanner != null) {
            mBanner.startAutoPlay();
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_location) {
            startActivity(new Intent(getContext(), SearchCityActivity.class));
        }
    }
}
